/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgHunt.cpp : implementation file

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgHunt.h"

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgHunt, CRgnDialog)

DlgHunt::DlgHunt(CWnd* pParent /*=NULL*/) : CRgnDialog(DlgHunt::IDD, pParent)
{
	skinIn = NULL;
	retVal = false;
	Initialize();
}

DlgHunt::~DlgHunt()
{
}

//---------------------------------------------------------------------------

void DlgHunt::DoDataExchange(CDataExchange* pDX)
{
	CRgnDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CHK_SYN,    chkSyn);
	DDX_Control(pDX, IDC_CHK_DNS,    chkDns);
	DDX_Control(pDX, IDC_CHK_LID,    chkLid);
	DDX_Control(pDX, IDC_EDT_DATA,   edtSyn);
	DDX_Control(pDX, IDC_STA_INFO,   staInfo);
	DDX_Control(pDX, IDC_BTN_OK,     btnOk);
	DDX_Control(pDX, IDC_BTN_CANCEL, btnCancel);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgHunt, CRgnDialog)
	ON_BN_CLICKED(IDC_CHK_SYN,    &DlgHunt::OnBnClickedChkSyn)
	ON_BN_CLICKED(IDC_BTN_OK,     &DlgHunt::OnBnClickedBtnOk)
	ON_BN_CLICKED(IDC_BTN_CANCEL, &DlgHunt::OnBnClickedBtnCancel)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

BOOL DlgHunt::OnInitDialog()
{
	CRgnDialog::OnInitDialog();

	SetProperties(skinIn, false, _T("Hunt parameters"));

	if(skin!=NULL){
		staInfo.textColor = skin->textColor;
		chkSyn.textColor  = skin->textColor;
		chkDns.textColor  = skin->textColor;
		chkLid.textColor  = skin->textColor;
	}

	staInfo.SetWindowText(_T("Press F1 to get help with these options"));

	DataToDisplay();

	return TRUE;
}

//---------------------------------------------------------------------------

void DlgHunt::OnOK()
{
	// CDialog::OnOK();
}

void DlgHunt::OnCancel()
{
	// CDialog::OnCancel();
}

//---------------------------------------------------------------------------

void DlgHunt::OnBnClickedChkSyn()
{
	bool state;

	state = chkSyn.GetCheck()==BST_CHECKED ? true : false;
	edtSyn.EnableWindow(state);
}

//---------------------------------------------------------------------------

void DlgHunt::OnBnClickedBtnOk()
{
	DisplayToData();
	retVal = true;
	EndDialog(IDOK);
}

void DlgHunt::OnBnClickedBtnCancel()
{
	retVal = false;
	EndDialog(IDABORT);
}

//---------------------------------------------------------------------------

void DlgHunt::ResizeChilds(void)
{
	int bw, bh;		///< size of button controls
	int eh;			///< size of edit and combobox controls
	int tw, th;		///< total required width and height
	int cw, ch;		///< client region's width and height
	int lh;
	int mx, my;		///< left and top space for displaying the objects in the center of the window
	int dd;			///< space between objects
	RECT rect;
	int posx, posy;

	dd = 2;

	btnOk.GetWindowRect(&rect);
	bw = rect.right - rect.left;
	bh = rect.bottom - rect.top;

	edtSyn.GetWindowRect(&rect);
	eh = rect.bottom - rect.top;

	cw = clientRect.right - clientRect.left;
	ch = clientRect.bottom - clientRect.top;

	tw = cw - 4*dd;
	th = 4*bh + 2*eh + 9*dd;

	mx = cw - tw; mx /= 2;
	my = ch - th; my /= 2;

	if(my<0) my = ch - th; // so that the buttons are visible !

	posx = clientRect.left + mx;
	posy = clientRect.top + my;

	chkSyn.MoveWindow(posx, posy, tw, bh, TRUE);
	posy += (dd + eh);
	edtSyn.MoveWindow(posx + eh, posy, tw - eh, bh, TRUE);
	posy += (dd + eh);
	chkDns.MoveWindow(posx, posy, tw, bh, TRUE);
	posy += (dd + eh);
	chkLid.MoveWindow(posx, posy, tw, bh, TRUE);
	posy += (dd + eh);

	lh = clientRect.bottom - 2*dd - bh - posy;
	staInfo.MoveWindow(posx, posy, tw, lh, TRUE);

	btnOk.MoveWindow(clientRect.right - mx - dd - 2*bw, clientRect.bottom - 2*dd - bh, bw, bh, TRUE);
	btnCancel.MoveWindow(clientRect.right - mx - bw, clientRect.bottom - 2*dd - bh, bw, bh, TRUE);
}

bool DlgHunt::GetMinimumSize(RECT* rectIn)
{
	if(rectIn==NULL) return false;

	rectIn->left   = 0;
	rectIn->right  = 250;
	rectIn->top    = 0;
	rectIn->bottom = 178;

	return true;
}

//---------------------------------------------------------------------------

void DlgHunt::Initialize(void)
{
	xHuntParams.Initialize();
}

void DlgHunt::DataToDisplay(void)
{
	edtSyn.SetWindowText(xHuntParams.xSynScanPorts);
	edtSyn.EnableWindow(xHuntParams.xPerformSynScan ? TRUE : FALSE);

	chkSyn.SetCheck(xHuntParams.xPerformSynScan ? BST_CHECKED : BST_UNCHECKED);

	chkDns.SetCheck(xHuntParams.xPerformDnsScan ? BST_CHECKED : BST_UNCHECKED);

	chkLid.SetCheck(xHuntParams.xFindLocation ?   BST_CHECKED : BST_UNCHECKED);
}

void DlgHunt::DisplayToData(void)
{
	xHuntParams.xPerformSynScan = chkSyn.GetCheck()==BST_CHECKED ? true : false;
	edtSyn.GetWindowText(xHuntParams.xSynScanPorts);
	xHuntParams.xPerformDnsScan = chkDns.GetCheck()==BST_CHECKED ? true : false;
	xHuntParams.xFindLocation   = chkLid.GetCheck()==BST_CHECKED ? true : false;
}

//---------------------------------------------------------------------------
